<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button id="btn">Click me</button>
  <script>
    // worker 由于安全策略,不能通过本地文件访问,需要开启服务
    // 默认使用commonjs规范,使用esm规范需要设置type="module"
    let worker = new Worker('./worker2.js', {
      // type:'module'
    });
    worker.onmessage = function (event) {
      console.log('Received message: ' + event.data);
    }
    document.getElementById('btn').addEventListener('click', function () {
      worker.postMessage('Hello');
    });
  </script>
</body>

</html>